package mum.edu.cs.waa.assignment.controller;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mum.edu.cs.waa.assignment.model.*;

/**
 *
 * @author teke
 */
@WebServlet(urlPatterns = {"/newpost"})
public class newpost extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String btn = request.getParameter("btn");
        String page;
        if (!btn.equalsIgnoreCase("Back")) {
            String title = request.getParameter("title");
            String category = request.getParameter("category");
            String txtpost = request.getParameter("txtpost");
            Account acc = (Account) request.getSession().getAttribute("Account");
            Post newpost = new Post(category, title, txtpost, acc);
            Database db = (Database) getServletContext().getAttribute("Database");
            db.getPosts().add(newpost);
            getServletContext().setAttribute("Database", db);

        }
        page = "post.jsp";
        RequestDispatcher topost = request.getRequestDispatcher(page);
        topost.forward(request, response);
    }

}
